/**
 * 
 */
package com.wolfpire.proms.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.wolfpire.proms.dao.ProjectIndexDao;
import com.wolfpire.proms.dao.TaskDao;
import com.wolfpire.proms.model.ProjectIndex;
import com.wolfpire.proms.service.BiReportService;
import com.wolfpire.system.common.EasyuiPage;
import com.wolfpire.system.common.Page;

/**
 * @author lihd
 *
 */

@Service("biReportService")
public class BiReportServiceImpl implements BiReportService {
	
	@Resource ProjectIndexDao projectIndexDao;
	
	@Resource TaskDao taskDao;

	@Override
	public List<Map<String, Object>> findTaskCount(Long projectIndexId) {
		List<Map<String, Object>> lists = new ArrayList<Map<String,Object>>();
		ProjectIndex projectIndex = new ProjectIndex();
		projectIndex.setParentId(projectIndexId);
		List<ProjectIndex> projectIndexIds = this.projectIndexDao.list(projectIndex);
		if (!CollectionUtils.isEmpty(projectIndexIds)) {
			for (ProjectIndex pi : projectIndexIds) {
				Long piId = pi.getId();
				if (piId.longValue() != projectIndexId.longValue()) {
					int count = this.taskDao.findTaskCount(piId);
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("piId", piId);
					map.put("name", pi.getName());
					map.put("value", count);
					map.put("leaf", pi.isLeaf());
					map.put("ppiId", projectIndexId);
					lists.add(map);
				}
			}
		} 
		return lists;
	}

	@Override
	public List<Map<String, Object>> findSumAmountUser(Long projectIndexId) {
		List<Map<String, Object>> lists = new ArrayList<Map<String,Object>>();
		ProjectIndex projectIndex = new ProjectIndex();
		projectIndex.setParentId(projectIndexId);
		List<ProjectIndex> projectIndexIds = this.projectIndexDao.list(projectIndex);
		if (!CollectionUtils.isEmpty(projectIndexIds)) {
			for (ProjectIndex pi : projectIndexIds) {
				Long piId = pi.getId();
				if (piId.longValue() != projectIndexId.longValue()) {
					String planAmount = this.taskDao.findTaskAmount(piId, "plan");
					String factAmount = this.taskDao.findTaskAmount(piId, "fact");
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("piId", piId);
					map.put("name", pi.getName());
					map.put("plan", planAmount);
					map.put("fact", factAmount);
					map.put("leaf", pi.isLeaf());
					map.put("ppiId", projectIndexId);
					lists.add(map);
				}
			}
		} 
		return lists;
	}

	@Override
	public List<Map<String, Object>> findCountTaskProgress(Long projectIndexId) {
		List<Map<String, Object>> lists = new ArrayList<Map<String,Object>>();
		ProjectIndex projectIndex = new ProjectIndex();
		projectIndex.setParentId(projectIndexId);
		List<ProjectIndex> projectIndexIds = this.projectIndexDao.list(projectIndex);
		if (!CollectionUtils.isEmpty(projectIndexIds)) {
			for (ProjectIndex pi : projectIndexIds) {
				Long piId = pi.getId();
				if (piId.longValue() != projectIndexId.longValue()) {
					Map<String, Object> taskProgresses = this.taskDao.findCountTaskProgress(piId); 
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("piId", piId);
					map.put("name", pi.getName());
					map.put("leaf", pi.isLeaf());
					map.put("ppiId", projectIndexId);
					
					map.put("level1", taskProgresses.get("level1"));
					map.put("level2", taskProgresses.get("level2"));
					map.put("level3", taskProgresses.get("level3"));
					map.put("level4", taskProgresses.get("level4"));
					map.put("level5", taskProgresses.get("level5"));
					lists.add(map);
				}
			}
		} 
		return lists;
	}

	@Override
	public List<Map<String, Object>> findWeightProgressData(Long projectIndexId) {
		List<Map<String, Object>> lists = new ArrayList<Map<String,Object>>();
		ProjectIndex projectIndex = new ProjectIndex();
		projectIndex.setParentId(projectIndexId);
		List<ProjectIndex> projectIndexIds = this.projectIndexDao.list(projectIndex);
		if (!CollectionUtils.isEmpty(projectIndexIds)) {
			for (ProjectIndex pi : projectIndexIds) {
				Long piId = pi.getId();
				if (piId.longValue() != projectIndexId.longValue()) {
					BigDecimal avgProgress = this.getAvgProgress(piId);
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("piId", piId);
					map.put("name", pi.getName());
					map.put("progress", avgProgress.toString());
					map.put("leaf", pi.isLeaf());
					map.put("ppiId", projectIndexId);
					lists.add(map);
				}
			}
		} 
		return lists;
	}

	private BigDecimal getAvgProgress(Long piId) {
		ProjectIndex projectIndex = new ProjectIndex();
		projectIndex.setParentId(piId);
		List<ProjectIndex> projectIndexIds = this.projectIndexDao.list(projectIndex);
		if (CollectionUtils.isNotEmpty(projectIndexIds)) {
			BigDecimal weight = new BigDecimal("0.00");
			String resProgressStr = "0.00";
			BigDecimal resProgress = new BigDecimal(resProgressStr);
			for (ProjectIndex pi : projectIndexIds) {
				String weightStr = pi.getWeight();
				if (StringUtils.isBlank(weightStr)) {
					weightStr = "1.00";
				}
				BigDecimal tempWeight = new BigDecimal(weightStr).setScale(3, 2);
				weight = weight.add(tempWeight).setScale(3, 2);
				BigDecimal avgprogress = this.getAvgProgress(pi.getId());
				resProgress = resProgress.add(tempWeight.multiply(avgprogress).setScale(3, 2)).setScale(3, 2);
			}
			return resProgress.divide(weight, 3).setScale(3, 2);
		} else {
			String resprogressStr = this.taskDao.findTaskAvgProgress(piId);
			return (new BigDecimal(resprogressStr)).setScale(3, 2);
		}
	}

	@Override
	public Page<Map<String, Object>> setTaskCountList(
			EasyuiPage<Map<String, Object>> page, Long projectIndexId, String type) {
		List<Map<String, Object>> lists = new ArrayList<Map<String,Object>>();
		ProjectIndex projectIndex = new ProjectIndex();
		projectIndex.setParentId(projectIndexId);
		List<ProjectIndex> projectIndexIds = this.projectIndexDao.list(projectIndex);
		if (!CollectionUtils.isEmpty(projectIndexIds)) {
			for (ProjectIndex pi : projectIndexIds) {
				Long piId = pi.getId();
				if (piId.longValue() != projectIndexId.longValue()) {
					Map<String, Object> map = this.taskDao.findCountTaskInfo(piId);
					map.put("project_index_id", piId);
					map.put("project_index_name", pi.getName());
					String projectIndexPlanAmount = pi.getPlanAmount();
					if (StringUtils.isBlank(projectIndexPlanAmount)) {
						projectIndexPlanAmount = "0.00";
					}
					map.put("pi_plan_amount_", projectIndexPlanAmount);
					BigDecimal piPlanAmount = new BigDecimal(projectIndexPlanAmount);
					BigDecimal planAmount = new BigDecimal("0.00");
					if (null != map.get("plan_amount_")) {
						planAmount = new BigDecimal(map.get("plan_amount_").toString()).setScale(3, 2);
					}
					BigDecimal unuserPiPlanAmount = piPlanAmount.subtract(planAmount).setScale(3, 2);
					map.put("unuser_pi_plan_amount_", unuserPiPlanAmount.toString());
					if ("1".equals(type)) {
						Map<String, Object> taskProgresses = this.taskDao.findCountTaskProgress(piId); 
						map.putAll(taskProgresses);
					}
					if ("2".equals(type)) {
						BigDecimal avgProgress = this.getAvgProgress(piId);
						map.put("progress", avgProgress.toString());
					}
					lists.add(map);
				}
			}
		}
		page.setDataList(lists);
		page.setRows(projectIndexIds.size() - 1);
		return page;
	}

	@Override
	public Page<Map<String, Object>> setProjectIndexMonitorDataList(
			EasyuiPage<Map<String, Object>> page, Map<String, Object> params) {
		// TODO Auto-generated method stub
		return page;
	}

}
